package il.ac.braude.sona.gui.forms;



import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.gui.SONAFrame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;



/**
 * This is an abstraction of the idea of Forms, a form is a set of questions with coresponding answers inputs,
 *  when the user submits it, form listners that have registered to an instance of this class will be informed 
 *  of the submittion and will be given all the answers the user has input, 
 *  this is conceptually similiar to Forms in HTML and in general.
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class FormFrame extends SONAFrame implements ActionListener, WindowListener {
	private FormPanel mFormPanel;
	private JPanel viewPort = new JPanel();
	private ArrayList<FormListener> mFormListeners = new ArrayList<FormListener>();
	private ExecutorService es = Executors.newSingleThreadExecutor();
	private JLabel errorLabel = new JLabel();

	public FormFrame(String title, String okButton) {
		setTitle(title);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setOpaque(false);
		JButton ok = new JButton(okButton);
		ok.addActionListener(this);
		buttonPanel.add(ok);
		getContentPane().add(buttonPanel,BorderLayout.SOUTH);
		viewPort.setOpaque(false);
		viewPort.setLayout(new BorderLayout());
		errorLabel.setVisible(false);
		errorLabel.setOpaque(false);
		errorLabel.setHorizontalAlignment(SwingConstants.CENTER);
		errorLabel.setVerticalAlignment(SwingConstants.CENTER);
		getContentPane().add(viewPort,BorderLayout.CENTER);
		addWindowListener(this);
		getRootPane().setDefaultButton(ok);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (mFormPanel == null) return;
		final ArrayList<Object> list = mFormPanel.getResult();
		if (list != null)
			es.execute(new Runnable() {
	
				@Override
				public void run() {
					for (FormListener l : mFormListeners)
						l.onFormSubmited(FormFrame.this,list);
				}
				
			});
		else 
			showMessege(Resources.Strings.NecesseryFieldsAreNotFilled);
	}
	
	/**
	 * sets the underlying form panel
	 * @param formPanel
	 */
	public void setFormPanel(FormPanel formPanel) {
		viewPort.removeAll();
		viewPort.add(mFormPanel=formPanel);
		viewPort.add(errorLabel,BorderLayout.SOUTH);
	}
	/**
	 * you can set the default values that the form should put when its displayed to the user
	 * @param arrayList
	 */
	public void setDefaultValues(ArrayList<String> arrayList) {
		mFormPanel.setDefaultValues(arrayList);
	}
	/**
	 * using this methos you can show mssg to the user on the bottom of this form
	 * @param tetx
	 */
	public void showMessege(String tetx) {
		errorLabel.setText(tetx);
		errorLabel.setVisible(true);
		pack();
	}
	/**
	 * make the mssg invisible
	 */
	public void hideMessege() {
		errorLabel.setVisible(false);
		pack();
	}
	/**
	 * add form listener, the new listener will be informed when user submits this form
	 * @param listener
	 */
	public void addFormListener(FormListener listener) {
		if (!mFormListeners.contains(listener)) mFormListeners.add(listener);
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		es.execute(new Runnable() {
			@Override
			public void run() {
				for (FormListener l : mFormListeners)
					l.onFormCanceled(FormFrame.this);
			}
		});
	}
	


	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
